using System.Text;

namespace RealDolmen.Helpers
{
    public class KeyManager
    {
        protected internal char Separator = '.';

        public KeyManager(char separator)
        {
            Separator = separator;
        }

        public string GenerateFullString(params object[] list)
        {
            return GenerateFullKey(Separator, list);
        }

        public string ExtractFirstKeyPart(string key)
        {
            return ExtractFirstKeyPart(Separator, key);
        }


        public static string GenerateFullKey(char separator, params object[] list)
        {
            if (list.Length > 0)
            {
                StringBuilder sb = new StringBuilder();

                sb.Append(list[0]);
                for (int i = 1; i < list.Length; i++)
                {
                    object val = list[i];

                    if (val != null)
                    {
                        string strVal = val.ToString();

                        if (strVal != string.Empty)
                        {
                            sb.Append(separator);
                            sb.Append(strVal);
                        }
                    }
                }
                return sb.ToString();
            }
            return string.Empty;
        }

        public static string ExtractFirstKeyPart(char separator, string key)
        {
            if (key != null)
            {
                int idx = key.IndexOf(separator);

                if (idx > 0)
                    key = key.Remove(idx);
            }
            
            return key;
        }


        private string prefix = string.Empty;
        public string Prefix
        {
            get { return prefix; }
            internal protected set { prefix = value; }
        }


        public KeyManager(char separator, string prefix)
            : this(separator)
        {
            Prefix = prefix;
        }

        public string GenerateKey(string internalName)
        {
            return GenerateFullString(Prefix, internalName);
        }

        public string ExtractInternalName(string key)
        {
            string name = ExtractKeyPartsAfter(Separator, key, Prefix);
            return ExtractFirstKeyPart(name);
        }

        public bool IsValidKey(string key)
        {
            return key.StartsWith(Prefix + char.ToString(Separator));
        }

        public static string ExtractKeyPartsAfter(char separator, string key, string prefix)
        {
            int idx = key.LastIndexOf(prefix + char.ToString(separator));

            if (idx < 0)
                return null;
            else
                return key.Substring(idx + prefix.Length + 1);
        }
    }

    public class SectionKeyManager : KeyManager
    {
        private KeyManager keyManager;
        private string section;

        public SectionKeyManager(KeyManager keyManager, string section)
            : base(keyManager.Separator)
        {
            this.keyManager = keyManager;
            Section = section;
        }

        public string Section
        {
            get { return section; }
            set { 
                section = value;
                Prefix = GenerateFullString(keyManager.Prefix, section);
            }
        }
    }
}
